<?php
class bFilter{
	private static $Instance;
	// just work with 2  categorys at once.
	private $Category_01;
	private $Category_02;
	private $preferable;
	private function __construct(bcategory $c1,bcategory $c2){
		$this->Category_01 = $c1;
		$this->Category_02 = $c2;
	}
	public function setPreferable(bcategory $p){
		$this->preferable = ($p === $this->Category_01)?1:2;
	}
	public function getInstance( bcategory $c1,bcategory $c2){
		if ( is_null(bFilter::$Instance))
			bFilter::$Instance = new bFilter( $c1, $c2);
		return bFilter::$Instance;
	}
	public function Analyze($content){
		$prob = Array(1=>0, 2=>0);
		$totaldoc = $this->Category_01->getTotalDoc()+ $this->Category_02->getTotalDoc();
		$words = split(' ',preg_replace(FILTER_EXPRESSION, " ",$content));
		foreach ($words as $word){
			if (strlen($word) <=IGNOREWORDEQUALSORLESSTHEN || $word=="")
				continue;
			$p_01 = ($this->Category_01->getWord($word)!=0)?$this->Category_01->getWord($word):BASICPROBABILITY;
			$p_02 = ($this->Category_02->getWord($word)!=0)?$this->Category_02->getWord($word):BASICPROBABILITY;
				$prob[1] = ($prob[1] == 0 )?$p_01:($prob[1]*$p_01);
				$prob[2] = ($prob[2] == 0 )?$p_02:($prob[2]*$p_02);
		}
		$prob[1] = $prob[1] * ($this->Category_01->getTotalDoc() / $totaldoc);
		$prob[2] = $prob[2] * ($this->Category_02->getTotalDoc() / $totaldoc);
		$prob['result'] =($prob[1]> $prob[2])?$this->Category_01->getType():$this->Category_02->getType();
		return $prob;
		
		
	}
}